package com.qiangesoft.bootcodegen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.bootcodegen.entity.BcgOperationLog;
import com.qiangesoft.bootcodegen.mapper.BcgOperationLogMapper;
import com.qiangesoft.bootcodegen.pojo.query.BcgOperationLogQuery;
import com.qiangesoft.bootcodegen.pojo.query.PageQuery;
import com.qiangesoft.bootcodegen.pojo.vo.PageResultVO;
import com.qiangesoft.bootcodegen.service.IBcgOperationLogService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 操作日志 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-04-01
 */
@Service
public class BcgOperationLogServiceImpl extends ServiceImpl<BcgOperationLogMapper, BcgOperationLog> implements IBcgOperationLogService {

    @Override
    public PageResultVO<BcgOperationLog> listOperationLog(PageQuery pageQuery, BcgOperationLogQuery operationLogQuery) {
        long pageNum = pageQuery.getPageNum();
        long pageSize = pageQuery.getPageSize();

        PageResultVO<BcgOperationLog> pageResult = new PageResultVO<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);

        LambdaQueryWrapper<BcgOperationLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(operationLogQuery.getOperationName()), BcgOperationLog::getOperationName, operationLogQuery.getOperationName())
                .ge(operationLogQuery.getStartTime() != null, BcgOperationLog::getCreateTime, operationLogQuery.getStartTime())
                .lt(operationLogQuery.getEndTime() != null, BcgOperationLog::getCreateTime, operationLogQuery.getEndTime())
                .orderByDesc(BcgOperationLog::getCreateTime);
        Page<BcgOperationLog> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        pageResult.setTotal(page.getTotal());
        pageResult.setPages(page.getPages());
        pageResult.setResults(page.getRecords());

        return pageResult;
    }
}
